<template>
  <MainSlot v-loading="loading">
    <template v-slot:searchForm>
      <SearchForm :formItemList="formItemList" @search="search"></SearchForm>
    </template>

    <template v-slot:btn>
      <BtnSlot :btnList="btnList" @handleEvent="getBtnEvent"></BtnSlot>
    </template>

    <template v-slot:table>
      <TableData :config="table_config" :tableThead="tableThead" :dataList="dataList" :loading="loading"
        @selection-change="selectionChange">
      </TableData>
    </template>

    <template v-slot:pagination>
      <Pagination @size-Change="handleSizeChange" :size="size" @current-change="handleCurrentChange" :page="page">
      </Pagination>
    </template>

    <template v-slot:dialogModule>
      <AddEdit :title="'巡查路线信息'" @closeDialog="closeDialog" :pointList="pointList" :addEditFlag="addEditFlag"
        :dialogVisible="dialogVisible" :data="formData">
      </AddEdit>
    </template>
  </MainSlot>
</template>

<script>
import MainSlot from "@/components/MainSlot";
import Pagination from "@/components/Pagination";
import SearchForm from "@/components/SearchForm";
import TableData from "@/components/TableData";
import BtnSlot from "@/components/BtnSlot";
import AddEdit from "./components/addEdit.vue";
import patrolLineManage from "@/api/patrol-info-manage/patrol-line-manage";

export default {
  components: { MainSlot, Pagination, SearchForm, TableData, BtnSlot, AddEdit },
  data() {
    return {
      loading: false,
      dialogVisible: false,
      addEditFlag: "add",
      formData: {},
      page: 1,
      size: 10,
      selectIndex: 0,
      btnList: ["add", "delete"],
      dataList: [],
      tableThead: [
        { isShow: true, label: "巡查线路名称", prop: "inspectionRouteName" },
      ],
      // 配置项
      table_config: {
        isCheckBox: true,

        isOperation: true,
        // 表格操作列
        operation: {
          label: "操作",
          width: "200",
          cols: [
            {
              label: "编辑",
              type: "primary",
              flag: "edit",
              handleRow: this.handleRow,
            },
            {
              label: "删除",
              type: "danger",
              flag: "delete",
              handleRow: this.handleRow,
            },
          ],
        },
      },
      pointList: [],
      formItemList: [
        {
          label: "巡查路线名称",
          type: "input",
          param: "inspectionRouteName",
          defaultSelect: "", // 下拉框默认选中项
          multiple: false,
          isShow: true,
        },
      ],
    };
  },
  mounted() {
    this.queryPageByCondition();
    this.initData();
  },

  methods: {
    async initData() {
      //获取养殖设施；
      let { data: pointList } =
        await patrolLineManage.getInspectionPointCondition({
          organizationNo: sessionStorage.organizationNo,
        });
      this.pointList = pointList || [];
    },
    delList(deleteList = [], key = "inspectionRouteId") {
      let list = [];
      deleteList.forEach((item) => {
        list.push(item[key]);
      });
      console.log(list);
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          patrolLineManage
            .delInspectionRouteInfo(list)
            .then((res) => {
              if (res.code == 1) {
                this.$message.success(res.msg || "删除成功");
                this.queryPageByCondition();
              } else {
                this.$message.warning(res.msg || "删除失败");
              }
            })
            .catch((err) => {
              this.$message.warning(err || err.msg || "删除失败");
            });
        })
        .catch((err) => {
          this.$message({
            type: "info",
            message: "取消删除",
          });
        });
    },
    queryPageByCondition() {
      let params = {
        page: this.page,
        size: this.size,
        entity: {
          ...this.searchFormData,
        },
      };
      patrolLineManage
        .getInspectionRouteList(params)
        .then((res) => {
          this.dataList = [];
          if (res.code == 1) {
            this.dataList = res.data.content || [];
            this.total = res.data.totalElements;
            if (res.data.totalPages && this.page > res.data.totalPages) {
              this.page = res.data.totalPages;
              this.queryPageByCondition();
            }
          } else {
            this.$message.warning(res.msg || "分页查询失败");
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    search(data) {
      this.page = 1;
      this.searchFormData = { ...this.searchFormData, ...data };
      this.queryPageByCondition();
    },
    handleCurrentChange(page) {
      this.page = page;
      this.queryPageByCondition();
    },
    handleSizeChange(size) {
      this.size = size;
      this.queryPageByCondition();
    },
    // 操作方法
    handleRow(rowData, item) {
      if (item.flag == "edit") {
        this.dialogVisible = true;
        this.formData = JSON.parse(JSON.stringify(rowData));
      } else if (item.flag == "delete") {
        this.delList([rowData]);
      }
      this.addEditFlag = item.flag;
    },
    // 多选方法
    selectionChange(val) {
      this.selectList = val || [];
    },
    //获取选中Btn
    getBtnEvent(flag) {
      console.log(flag);
      if (flag == "add") {
        this.dialogVisible = true;
      } else if (flag == "delete") {
        if (this.selectList.length > 0) {
          this.delList(this.selectList);
        } else {
          this.$message.warning("请先选择表单数据项后再删除！");
        }
      }
      this.addEditFlag = flag;
    },
    //关闭弹窗
    closeDialog() {
      this.dialogVisible = false;
      this.queryPageByCondition();
    },

    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => { });
    },
  },
};
</script>

<style lang="scss" scoped>
.pointer {
  cursor: pointer;
}
</style>